# coding: utf-8
from PyQt5.QtCore import QUrl, QSize
from PyQt5.QtGui import QIcon, QColor, QPixmap
from PyQt5.QtWidgets import QApplication

from qfluentwidgets import NavigationItemPosition, MSFluentWindow, SplashScreen
from qfluentwidgets import FluentIcon as FIF

from .setting_interface import SettingInterface
from .game_interface import GameInterface
from .mod_interface import ModInterface
from .map_interface import MapInterface
from .skin_interface import SkinInterface
from .pack_interface import PackInterface
from .resourceInfo_interface import ResourceDetailsInterface
from ..common.config import cfg
from ..common.icon import Icon
from ..common.signal_bus import signalBus
from ..common import resource


class MainWindow(MSFluentWindow):

    def __init__(self):
        super().__init__()
        self.initWindow()

        # TODO: create sub interface

        self.gameInterface = GameInterface(self)
        self.modInterface = ModInterface(self)
        self.mapInterface = MapInterface(self)
        self.skinInterface = SkinInterface(self)
        self.packInterface = PackInterface(self)
        self.settingInterface = SettingInterface(self)

        self.connectSignalToSlot()

        # add items to navigation interface
        self.initNavigation()

    def connectSignalToSlot(self):
        signalBus.micaEnableChanged.connect(self.setMicaEffectEnabled)

    def initNavigation(self):

        self.addSubInterface(
            self.gameInterface,
            QIcon(":app/images/game.png"),
            self.tr("Minecraft"),
            QIcon(":app/images/game.png"),
            NavigationItemPosition.TOP,
        )
        self.addSubInterface(
            self.modInterface,
            QIcon(":app/images/mod.png"),
            self.tr("Mod"),
            QIcon(":app/images/mod.png"),
            NavigationItemPosition.TOP,
        )
        self.addSubInterface(
            self.mapInterface,
            QIcon(":app/images/map.png"),
            self.tr("Map"),
            QIcon(":app/images/map.png"),
            NavigationItemPosition.TOP,
        )
        self.addSubInterface(
            self.skinInterface,
            QIcon(":app/images/skin.png"),
            self.tr("Skin"),
            QIcon(":app/images/skin.png"),
            NavigationItemPosition.TOP,
        )
        self.addSubInterface(
            self.packInterface,
            QIcon(":app/images/textual.png"),
            self.tr("Textual"),
            QIcon(":app/images/textual.png"),
            NavigationItemPosition.TOP,
        )

        # add custom widget to bottom
        self.addSubInterface(
            self.settingInterface,
            QIcon(":app/images/setting.png"),
            self.tr("Settings"),
            QIcon(":app/images/setting.png"),
            NavigationItemPosition.BOTTOM,
        )

        self.splashScreen.finish()

    def initWindow(self):
        self.resize(1100, 680)
        self.setMinimumSize(1100, 680)
        self.setWindowIcon(QIcon(":/app/images/logo.png"))
        self.setWindowTitle("XZWMC")

        self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))

        # create splash screen
        self.splashScreen = SplashScreen(self.windowIcon(), self)
        self.splashScreen.setIconSize(QSize(106, 106))
        self.splashScreen.raise_()

        desktop = QApplication.primaryScreen().availableGeometry()
        w, h = desktop.width(), desktop.height()
        self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)
        self.show()
        QApplication.processEvents()

    def resizeEvent(self, e):
        super().resizeEvent(e)
        if hasattr(self, "splashScreen"):
            self.splashScreen.resize(self.size())

    def addInterface(self, interface, icon, text, position):
        self.addSubInterface(
            interface, icon=icon, text=text, selectedIcon=icon, position=position
        )
